#include "stdafx.h"
#include "wf_polylineadd.h"

#include "pf_polyline.h"
#include "pf_snapper.h"



WF_PolylineAdd::WF_PolylineAdd(PF_Container& container,
	PF_GraphicView& graphicView)
	:PF_PreviewActionInterface("Add node",
			   container, graphicView) {}


QAction* WF_PolylineAdd::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	QAction* action = new QAction(tr("&Add node"), NULL);
	action->setShortcut(QKeySequence());
	action->setStatusTip(tr("Add polyline's node"));
	return action;
}

void WF_PolylineAdd::init(int status) {
	PF_ActionInterface::init(status);
	addEntity = addSegment = NULL;
	addCoord = PF_Vector(false);
}



void WF_PolylineAdd::trigger() {

	PF_PreviewActionInterface::trigger();
	PF_DEBUG->print("PF_ActionPolylineAdd::trigger()");

	if (addEntity!=NULL && addSegment->isAtomic() && addCoord.valid &&
		addSegment->isPointOnEntity(addCoord)) {

		addEntity->setHighlighted(false);
		graphicView->drawEntity(addEntity);

		PF_Modification m(*container, graphicView);
		addEntity = m.addPolylineNode((PF_Polyline&)*addEntity, (PF_AtomicEntity&)*addSegment, addCoord );

		addCoord = PF_Vector(false);

		PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
	}
////////////////////////////////////////2006/06/15
		graphicView->redraw(LX_Define::RedrawDrawing);
////////////////////////////////////////
}



void WF_PolylineAdd::mouseMoveEvent(PF_MouseEvent* e) {
	PF_DEBUG->print("PF_ActionPolylineAdd::mouseMoveEvent begin");

	switch (getStatus()) {
	case ChooseSegment:
		break;
	case SetAddCoord:
		snapPoint(e);
		break;
	default:
		break;
	}

	PF_DEBUG->print("PF_ActionPolylineAdd::mouseMoveEvent end");
}



void WF_PolylineAdd::mouseReleaseEvent(PF_MouseEvent* e) {
        if (e->button()==Qt::LeftButton) {
		switch (getStatus()) {
		case ChooseSegment:
			addEntity = catchEntity(e);
			if (addEntity==NULL) {
			PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
			} else if (addEntity->rtti()!=LX_Define::EntityPolyline) {

			PF_DIALOGFACTORY->commandMessage(
				tr("Entity must be a polyline."));
			} else {
				addEntity->setHighlighted(true);
				graphicView->drawEntity(addEntity);
				setStatus(SetAddCoord);
////////////////////////////////////////2006/06/15
				graphicView->redraw(LX_Define::RedrawDrawing);
////////////////////////////////////////
			}
			break;

		case SetAddCoord:
			addCoord = snapPoint(e);
			if (addEntity==NULL) {
				PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
			} else if (!addCoord.valid) {
				PF_DIALOGFACTORY->commandMessage(tr("Adding point is invalid."));
			} else {
				PF_Vector clickCoord = snapPoint(e);
				addSegment = NULL;
				double dist = graphicView->toGraphDX(snapRange)*0.9;
				addSegment =  ((PF_Polyline*)addEntity)->getNearestEntity( clickCoord, &dist, LX_Define::ResolveNone);
				if(addSegment == NULL) {
					PF_DIALOGFACTORY->commandMessage(
							tr("Adding point is not on entity."));
					break;
				}
				deleteSnapper();
				trigger();
			}
			break;

		default:
			break;
		}
        } else if (e->button()==Qt::RightButton) {
		deleteSnapper();
		if (addEntity!=NULL) {
			addEntity->setHighlighted(false);
			graphicView->drawEntity(addEntity);
////////////////////////////////////////2006/06/15
			graphicView->redraw(LX_Define::RedrawDrawing);
////////////////////////////////////////
		}
		init(getStatus()-1);
	}
}


void WF_PolylineAdd::updateMouseButtonHints() {
	switch (getStatus()) {
	case ChooseSegment:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify polyline to add nodes"),
						tr("Cancel"));
		break;
	case SetAddCoord:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify adding node's point"),
						tr("Back"));
		break;
	default:
		PF_DIALOGFACTORY->updateMouseWidget("", "");
		break;
	}
}



void WF_PolylineAdd::updateMouseCursor() {
	graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_PolylineAdd::updateToolBar() {
	switch (getStatus()) {
	case SetAddCoord:
	case ChooseSegment:
		PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
		break;
	default:
		PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarPolylines);
		break;
	}
}


// EOF
